/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.embed.domain.service;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.embed.domain.feign.DataCenterFeign;
import org.cshoo.tattoo.embed.domain.feign.dto.QuerySingleConfigResp;
import org.cshoo.tattoo.embed.domain.feign.dto.QuerySingleLookupItemResp;
import org.cshoo.tattoo.embed.domain.feign.dto.QuerySingleLookupResp;
import org.cshoo.tattoo.embed.domain.feign.dto.QuerySingleMessageResp;
import org.cshoo.tattoo.embed.infra.exception.FrequentCallException;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
@Slf4j
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class DataCenterService {

    @Qualifier("dataCenterFeign")
    @Resource
    private DataCenterFeign dataCenterFeign;

    @Cacheable(value = "getConfigValue", cacheManager = "caffeineCacheManager")
    public QuerySingleConfigResp getConfigValue(String code) throws Exception {
        log.info("code:{}", code);
        return dataCenterFeign.querySingleConfig(code);
    }

    @Cacheable(value = "querySingleLookup", cacheManager = "caffeineCacheManager")
    public QuerySingleLookupResp querySingleLookup(String code) throws Exception {
        log.info("code:{}", code);
        return dataCenterFeign.querySingleLookup(code);
    }

    @Cacheable(value = "querySingleMessage", cacheManager = "caffeineCacheManager")
    public QuerySingleMessageResp querySingleMessage(String code) throws Exception {
        log.info("code:{}", code);
        return dataCenterFeign.querySingleMessage(code);
    }

    /**
     * 获取lookup item的值
     *
     * @param lookupCode lookup编号
     * @param itemCode   lookup item编号
     * @param tClass     类型
     * @param <T>        泛型
     * @return lookup item的值
     * @throws FrequentCallException 数据中心异常
     */
    public <T> T getLookupItemValue(String lookupCode, String itemCode, Class<T> tClass) throws Exception {
        DataCenterService dataCenterService = (DataCenterService) AopContext.currentProxy();
        QuerySingleLookupResp querySingleLookup = dataCenterService.querySingleLookup(lookupCode);
        log.info("querySingleLookup:{}", querySingleLookup);
        List<QuerySingleLookupItemResp> querySingleConfigItems = querySingleLookup.getLookupItems();
        Optional<QuerySingleLookupItemResp> lookupItemOptional = querySingleConfigItems.stream()
                .filter(item -> item.getCode().equals(itemCode))
                .findAny();
        if (lookupItemOptional.isPresent()) {
            QuerySingleLookupItemResp lookupItemResp = lookupItemOptional.get();
            String value = lookupItemResp.getValue();
            return tClass.getConstructor(String.class).newInstance(value);
        }
        return null;
    }
}
